#include "changepasswordwidget.h"


ChangePasswordWidget::ChangePasswordWidget(QWidget *parent) : QWidget(parent)
{
    titleLabel = new QLabel("修改密码");
    titleLabel->setFixedHeight(20);
    titleLabel->setFrameStyle(QLabel::StyledPanel);
    titleLabel->setAlignment(Qt::AlignCenter);

    verifyWidget = new QWidget(this);
    verifyLabel = new QLabel("请先验证身份");
    verifyLabel->setFixedHeight(40);
    verifyLabel->setFrameStyle(QLabel::StyledPanel);
    verifyLabel->setAlignment(Qt::AlignCenter);
    usernameLabel = new QLabel("用户名：");
    usernameLineEdit = new QLineEdit;
    passwordLabel = new QLabel("密码：");
    passwordLineEdit = new QLineEdit;
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    verifyOkButton = new QPushButton("验证");
    verifyOkButton->setEnabled(false);
    QGridLayout* verifyLayout = new QGridLayout(verifyWidget);
    verifyLayout->addWidget(verifyLabel, 0, 0, 1, 5);
    verifyLayout->addWidget(usernameLabel, 1, 1, 1, 1);
    verifyLayout->addWidget(usernameLineEdit, 1, 2, 1, 2);
    verifyLayout->addWidget(passwordLabel, 2, 1, 1, 1);
    verifyLayout->addWidget(passwordLineEdit, 2, 2, 1, 2);
    verifyLayout->addWidget(verifyOkButton, 4, 2, 1, 1);

    changeWidget = new QWidget(this);
    changeLabel = new QLabel("请输入新密码");
    changeLabel->setFixedHeight(40);
    changeLabel->setFrameStyle(QLabel::StyledPanel);
    changeLabel->setAlignment(Qt::AlignCenter);
    newPasswordLabel = new QLabel("新密码：");
    newPasswordLineEdit = new QLineEdit;
    newPasswordLineEdit->setEchoMode(QLineEdit::Password);
    confirmPasswordLabel = new QLabel("再输一次：");
    confirmPasswordLineEdit = new QLineEdit;
    confirmPasswordLineEdit->setEchoMode(QLineEdit::Password);
    confirmHintLabel = new QLabel("");
    changeOkButton = new QPushButton("确认");
    changeOkButton->setEnabled(false);
    changeCancelButton = new QPushButton("取消");
    QGridLayout* changeLayout = new QGridLayout(changeWidget);
    changeLayout->addWidget(changeLabel, 0, 0, 1, 5);
    changeLayout->addWidget(newPasswordLabel, 1, 1, 1, 1);
    changeLayout->addWidget(newPasswordLineEdit, 1, 2, 1, 2);
    changeLayout->addWidget(confirmPasswordLabel, 2, 1, 1, 1);
    changeLayout->addWidget(confirmPasswordLineEdit, 2, 2, 1, 2);
    changeLayout->addWidget(confirmHintLabel, 2, 4, 1, 1);
    changeLayout->addWidget(changeOkButton, 4, 1, 1, 1);
    changeLayout->addWidget(changeCancelButton, 4, 3, 1, 1);
    changeWidget->hide();

    QVBoxLayout* topLayout = new QVBoxLayout(this);
    topLayout->addSpacing(20);
    topLayout->addWidget(titleLabel);
    topLayout->addWidget(verifyWidget);
    topLayout->addWidget(changeWidget);

    connect(usernameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotVerifyLineEditTextChange()));
    connect(passwordLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotVerifyLineEditTextChange()));
    connect(verifyOkButton, SIGNAL(clicked(bool)), this, SLOT(slotVerifyOkButton()));
    connect(newPasswordLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotChangeLineEditTextChange()));
    connect(confirmPasswordLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotChangeLineEditTextChange()));
    connect(changeCancelButton, SIGNAL(clicked(bool)), this, SLOT(slotChangeCancelButton()));
    connect(changeOkButton, SIGNAL(clicked(bool)), this, SLOT(slotChangeOkButton()));
}

void ChangePasswordWidget::slotVerifyOkButton()
{
    username = usernameLineEdit->text();
    QSqlQuery query;
    query.exec(QString("select password from passwordtable where username='%1'").arg(username));
    query.next();
    if(query.value(0).toString() == passwordLineEdit->text())
    {
        QMessageBox::information(this, "提示", "验证成功！", QMessageBox::Ok);
        verifyWidget->hide();
        changeWidget->show();
    }
    else
    {
        QMessageBox::warning(this, "警告", "帐号与密码不符，验证失败！", QMessageBox::Ok);
    }
}
void ChangePasswordWidget::slotVerifyLineEditTextChange()
{
    if(!usernameLineEdit->text().isEmpty() && !passwordLineEdit->text().isEmpty())
    {
        verifyOkButton->setEnabled(true);
    }
    else
    {
        verifyOkButton->setEnabled(false);
    }
}
void ChangePasswordWidget::slotChangeOkButton()
{
    QSqlQuery query;
    query.exec(QString("update passwordtable set password='%1' where username='%2'").arg(newPasswordLineEdit->text()).arg(username));
    QMessageBox::information(this, "提示", "修改成功！", QMessageBox::Ok);
    slotChangeCancelButton();
}
void ChangePasswordWidget::slotChangeCancelButton()
{
    usernameLineEdit->clear();
    passwordLineEdit->clear();
    newPasswordLineEdit->clear();
    confirmPasswordLineEdit->clear();
    confirmHintLabel->setText("");
    verifyOkButton->setEnabled(false);
    changeOkButton->setEnabled(false);
    changeWidget->hide();
    verifyWidget->show();
}
void ChangePasswordWidget::slotChangeLineEditTextChange()
{
    if(!newPasswordLineEdit->text().isEmpty() && !confirmPasswordLineEdit->text().isEmpty())
    {
        if(newPasswordLineEdit->text() == confirmPasswordLineEdit->text())
        {
            changeOkButton->setEnabled(true);
            confirmHintLabel->setText("✔一致");
            confirmHintLabel->setStyleSheet("color:green;");
        }
        else
        {
            changeOkButton->setEnabled(false);
            confirmHintLabel->setText("✖不一致");
            confirmHintLabel->setStyleSheet("color:red;");
        }

    }
    else    //有空的
    {
        changeOkButton->setEnabled(false);
        confirmHintLabel->setText("");
    }
}
